package impl

import (
	"adam2/internal/constants"
	"adam2/internal/dto"
	"adam2/internal/vo"
	"strings"
	"time"
)

type ModelTopConceptBoardIndexDetailDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetModelTopConceptBoardIndexDetailDaoImpl() *ModelTopConceptBoardIndexDetailDaoImpl {
	return &ModelTopConceptBoardIndexDetailDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (_modelTopConceptBoardIndexDetailDaoImpl *ModelTopConceptBoardIndexDetailDaoImpl) FindTableName() string {
	return "MDL_TOP_CNCPT_BOARD_IDX_DETAIL"
}

// 分页显示
func (_modelTopConceptBoardIndexDetailDaoImpl *ModelTopConceptBoardIndexDetailDaoImpl) Page(orderByColumnArray []vo.OrderByColumn, transactionDate time.Time, pageNo int, pageSize int) dto.ModelTopConceptBoardIndexDetailDtoArray {
	var modelTopConceptBoardIndexDetailDtoArray dto.ModelTopConceptBoardIndexDetailDtoArray
	var offset int = (pageNo - 1) * pageSize
	db := _modelTopConceptBoardIndexDetailDaoImpl.db.Table("mdl_top_cncpt_board_idx_detail t")
	db.Distinct().Select("t.ID as ID, CLOSE_PRICE as CLOSE_PRICE, " +
		"to_char(t.TRANSACTION_DATE, 'yyyy-mm-dd') as TRANSACTION_DATE, t.MYQUANT_CODE as MYQUANT_CODE, " +
		"t.AKSHARE_CODE as AKSHARE_CODE, t.NAME as NAME, " +
		"t.UP_DOWN_PERCENTAGE_2 as UP_DOWN_PERCENTAGE_2, t.UP_DOWN_PERCENTAGE_3 as UP_DOWN_PERCENTAGE_3, " +
		"t.UP_DOWN_PERCENTAGE_4 as UP_DOWN_PERCENTAGE_4, t.UP_DOWN_PERCENTAGE_5 as UP_DOWN_PERCENTAGE_5, " +
		"t.UP_DOWN_PERCENTAGE_10 as UP_DOWN_PERCENTAGE_10, t.UP_DOWN_PERCENTAGE_20 as UP_DOWN_PERCENTAGE_20")
	db.Joins("join CONCEPT_BOARD_INDEX cbi on cbi.MYQUANT_CODE=t.MYQUANT_CODE")
	if nil != orderByColumnArray && len(orderByColumnArray) > 0 {
		var orderBy strings.Builder
		for _, orderByColumn := range orderByColumnArray {
			orderBy.WriteString(orderByColumn.ColumnName)
			orderBy.WriteString(" ")
			if orderByColumn.AscOrDesc == constants.ASC {
				orderBy.WriteString(constants.ASC)
			}
			if orderByColumn.AscOrDesc == constants.DESC {
				orderBy.WriteString(constants.DESC)
			}
			orderBy.WriteString(",")
		}
		var order string = orderBy.String()[0 : len(orderBy.String())-1]
		db = db.Order(order)
	}
	db = db.Offset(offset).Limit(pageSize)
	if !transactionDate.IsZero() {
		db = db.Where("t.TRANSACTION_DATE=?", transactionDate).
			Where("cbi.TRANSACTION_DATE=?", transactionDate)
	}
	db.Find(&modelTopConceptBoardIndexDetailDtoArray)
	return modelTopConceptBoardIndexDetailDtoArray
}

// 分页显示，返回总数
func (_modelTopConceptBoardIndexDetailDaoImpl *ModelTopConceptBoardIndexDetailDaoImpl) PageTotal(transactionDate time.Time) int {
	var modelTopConceptBoardIndexDetailDtoArray dto.ModelTopConceptBoardIndexDetailDtoArray
	db := _modelTopConceptBoardIndexDetailDaoImpl.db.Table("mdl_top_cncpt_board_idx_detail t")
	db.Select("t.ID as ID, CLOSE_PRICE as CLOSE_PRICE, " +
		"to_char(t.TRANSACTION_DATE, 'yyyy-mm-dd') as TRANSACTION_DATE, t.AKSHARE_CODE as AKSHARE_CODE, " +
		"t.MYQUANT_CODE as MYQUANT_CODE, t.NAME as NAME, " +
		"t.UP_DOWN_PERCENTAGE_2 as UP_DOWN_PERCENTAGE_2, t.UP_DOWN_PERCENTAGE_3 as UP_DOWN_PERCENTAGE_3, " +
		"t.UP_DOWN_PERCENTAGE_4 as UP_DOWN_PERCENTAGE_4, t.UP_DOWN_PERCENTAGE_5 as UP_DOWN_PERCENTAGE_5, " +
		"t.UP_DOWN_PERCENTAGE_10 as UP_DOWN_PERCENTAGE_10, t.UP_DOWN_PERCENTAGE_20 as UP_DOWN_PERCENTAGE_20")
	db.Joins("join CONCEPT_BOARD_INDEX cbi on cbi.MYQUANT_CODE=t.MYQUANT_CODE")
	if !transactionDate.IsZero() {
		db = db.Where("t.TRANSACTION_DATE=?", transactionDate).
			Where("cbi.TRANSACTION_DATE=?", transactionDate)
	}
	db.Find(&modelTopConceptBoardIndexDetailDtoArray)
	return len(modelTopConceptBoardIndexDetailDtoArray)
}
